package ac.you.api.ask.e;

import ac.you.api.AcYouAPI;
import org.bukkit.Bukkit;

import java.util.concurrent.CompletableFuture;

public final class ResultHandler<T>{
    private final Result<T> res;
    private Tasker<T> tasker;
    private CompletableFuture<T> handle;
    private boolean begin;
    private ResultHandler(Result<T> res) {
        this.res = res;
        this.begin = false;
    }
    public static <T> ResultHandler<T> build(Result<T> res){
        return new ResultHandler<>(res);
    }
    public final void init(CompletableFuture<T> future){
        this.handle = future;
        this.begin = true;
        Bukkit.getPluginManager().registerEvents(tasker, AcYouAPI.INSTANCE);
    }
    public final void tK(Tasker<T> tasker){
        if (this.tasker == null || !this.tasker.getAsk().isRunning()){
            this.tasker = tasker;
        }
    }
    public void finish(T t) {
        handle.complete(t);
        res.result(tasker,t);
    }
    public boolean isBegin(){
        return begin;
    }
    public CompletableFuture<T> value(){
        return handle;
    }
}
